package com.wangzaiplus.test;

import java.util.concurrent.CountDownLatch;

/**
 * @ClassName ThirdThread
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/04 11:03
 * @Version V1.0
 **/
public class ThirdThread implements Runnable {
    private final CountDownLatch latch;

    public ThirdThread(CountDownLatch latch) {
        this.latch = latch;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        System.out.println("the third thread is running!");
        //计算器-1
        latch.countDown();
        System.out.println("ThirdThread----------->count=" + latch.getCount());
    }

    public static void main(String[] args) throws InterruptedException {
        //FirstThread,SecondThread与ThirdThread实现完全相同
        //构造线程计数器
        final CountDownLatch latch = new CountDownLatch(3);
        Thread t1 = new Thread(new FirstThread(latch));
        Thread t2 = new Thread(new SecondThread(latch));
        Thread t3 = new Thread(new ThirdThread(latch));
        t1.start();
        t1.join();
        t2.start();
        t2.join();
        t3.start();
        //线程计数器技术为0时，继续执行主线程
        latch.await();
        System.out.println("the main thread is End...");
    }


}
class FirstThread implements Runnable {
    private final CountDownLatch latch;

    public FirstThread(CountDownLatch latch) {
        this.latch = latch;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        System.out.println("the FirstThread thread is running!");
        //计算器-1
        latch.countDown();
        System.out.println("FirstThread----------->count=" + latch.getCount());
    }
}

class SecondThread implements Runnable {
    private final CountDownLatch latch;

    public SecondThread(CountDownLatch latch) {
        this.latch = latch;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    public void run() {
        System.out.println("the SecondThread thread is running!");
        //计算器-1
        latch.countDown();
        System.out.println("SecondThread----------->count=" + latch.getCount());
    }
}